<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: gift_cart.php 327 2013-09-28 10:34:00Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 327 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-28 10:34:00 +0000 (Sat, 28 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

$gift_smarty = new Smarty;
$gift_smarty->assign('tpl_path', 'templates/'.CURRENT_TEMPLATE.'/');
require_once (DIR_FS_INC.'inc.date_short.php');

if(ACTIVATE_GIFT_SYSTEM == 'true')
	$gift_smarty->assign('ACTIVATE_GIFT', 'true');

if(isset($_SESSION['customer_id'])) {
	$gv_result = $db->db_query("SELECT amount FROM ".TABLE_COUPON_GV_CUSTOMER." WHERE customer_id = '".$_SESSION['customer_id']."'");
	if ($gv_result->fields['amount'] > 0) {
		$gift_smarty->assign('GV_AMOUNT', $price->format($gv_result->fields['amount'], true, 0, true));
		$gift_smarty->assign('GV_SEND_TO_FRIEND_LINK', href_link(FILENAME_GV_SEND));
	} else
		$gift_smarty->assign('GV_AMOUNT', 0);
}

if(isset($_SESSION['gv_id'])) {
	$coupon = $db->db_query("SELECT coupon_amount FROM ".TABLE_COUPONS." WHERE coupon_id = '".$_SESSION['gv_id']."'");
	$gift_smarty->assign('COUPON_AMOUNT2', $price->format($coupon->fields['coupon_amount'], true, 0, true));
}

if(isset($_SESSION['cc_id'])) {
	// Coupon mit Wert ausgeben
	$coupon =$db->db_query("SELECT * FROM ".TABLE_COUPONS." WHERE coupon_id = '".(int)$_SESSION['cc_id']."'");

	$coupon_desc = $db->db_query("SELECT * FROM ".TABLE_COUPONS_DESCRIPTION." WHERE coupon_id = '".(int)$_SESSION['cc_id']."' AND language_id = '".$_SESSION['languages_id']."'");

	$text_coupon_help = TEXT_COUPON_HELP_HEADER;
	$text_coupon_help .= sprintf(TEXT_COUPON_HELP_NAME, $coupon_desc->fields['coupon_name']);

	if(not_null($coupon_desc->fields['coupon_description']))
		$text_coupon_help .= sprintf(TEXT_COUPON_HELP_DESC, $coupon_desc->fields['coupon_description']);

	$coupon_amount = $coupon->fields['coupon_amount'];

	switch ($coupon->fields['coupon_type']) {
		case 'F' :
			$text_coupon_help .= sprintf(TEXT_COUPON_HELP_FIXED, $price->format($coupon->fields['coupon_amount'], true));
			break;
		case 'P' :
			$text_coupon_help .= sprintf(TEXT_COUPON_HELP_FIXED, number_format($coupon->fields['coupon_amount'], 2).'%');
			break;
		case 'S' :
			$text_coupon_help .= TEXT_COUPON_HELP_FREESHIP;
			break;
	}

	if ($coupon->fields['coupon_minimum_order'] > 0)
		$text_coupon_help .= sprintf(TEXT_COUPON_HELP_MINORDER, $price->format($coupon->fields['coupon_minimum_order'], true));

	$text_coupon_help .= sprintf(TEXT_COUPON_HELP_DATE, date_short($coupon->fields['coupon_start_date']), date_short($coupon->fields['coupon_expire_date']));

  $text_coupon_help_cat_restrict = '';
	$get_result = $db->db_query("SELECT
									restrict_to_categories
								FROM
									".TABLE_COUPONS."
								WHERE
									coupon_id = '".(int)$_SESSION['cc_id']."'");

	$cat_ids = explode(',', $get_result->fields['restrict_to_categories']);
	for($i = 0; $i < count($cat_ids); $i ++) {
		$result = $db->db_query("SELECT
									*
								FROM
									".TABLE_CATEGORIES." c,
									".TABLE_CATEGORIES_DESCRIPTION." cd
								WHERE
									c.categories_id = cd.categories_id
								AND
									cd.language_id = '".$_SESSION['languages_id']."'
								AND
									c.categories_id = '".$cat_ids[$i]."'");
		if($result->_numOfRows > 0)
			$text_coupon_help_cat_restrict .= '<br />'.$result->fields["categories_name"];
	}
	if ($text_coupon_help_cat_restrict !== '')
	 $text_coupon_help_cat_restrict = '<br /><br />'.TEXT_COUPON_HELP_CATEGORIES.$text_coupon_help_cat_restrict;

  $text_coupon_help_prod_restrict = '';
	$get_result = $db->db_query("SELECT
									restrict_to_products
								FROM
									".TABLE_COUPONS."
								WHERE
									coupon_id = '".(int)$_SESSION['cc_id']."'");

	$pr_ids = explode(',', $get_result->fields['restrict_to_products']);
	for ($i = 0; $i < count($pr_ids); $i ++) {
		$result = $db->db_query("SELECT
										*
									FROM
										".TABLE_PRODUCTS." p,
										".TABLE_PRODUCTS_DESCRIPTION." pd
									WHERE
										p.products_id = pd.products_id
									AND
										pd.language_id = '".$_SESSION['languages_id']."'
									AND
										p.products_id = '".$pr_ids[$i]."'");
		if($result->_numOfRows > 0)
			$text_coupon_help_prod_restrict .= '<br />'.$result->fields["products_name"];
	}
	if ($text_coupon_help_prod_restrict !== '')
	 $text_coupon_help_prod_restrict = '<br /><br />'.TEXT_COUPON_HELP_PRODUCTS.$text_coupon_help_prod_restrict;

  if ($text_coupon_help_cat_restrict.$text_coupon_help_prod_restrict !== '') {
  	$text_coupon_help .= '<b>'.TEXT_COUPON_HELP_RESTRICT.'</b>'
  	                   . $text_coupon_help_cat_restrict
  	                   . $text_coupon_help_prod_restrict;
  }
	$gift_smarty->assign('TEXT_COUPON_ACCEPT_INFO',$text_coupon_help);
	$gift_smarty->assign('COUPON_HELP_LINK', '<a href="javascript:void(0)" onclick="javascript:window.open(\''.href_link(FILENAME_POPUP_COUPON_HELP, 'cID='.$_SESSION['cc_id']).'\', \'popup\', \'toolbar=0,scrollbars=yes, width=350, height=350\')">');

}
if(isset($_SESSION['customer_id']))
	$gift_smarty->assign('C_FLAG', 'true');

$gift_smarty->assign('LINK_ACCOUNT', href_link(FILENAME_CREATE_ACCOUNT, '', 'SSL'));
$gift_smarty->assign('FORM_ACTION', draw_form('gift_coupon', href_link(FILENAME_SHOPPING_CART, 'action=check_gift', 'NONSSL')));
$gift_smarty->assign('INPUT_CODE', draw_input_field('gv_redeem_code'));
$gift_smarty->assign('BUTTON_SUBMIT', image_submit('button_redeem.gif', IMAGE_REDEEM_GIFT));
$gift_smarty->assign('language', $_SESSION['language']);
$gift_smarty->assign('FORM_END', '</form>');
$gift_smarty->caching = false;
$smarty->assign('MODULE_gift_cart', $gift_smarty->fetch(CURRENT_TEMPLATE.'/module/gift_cart.html'));
?>